// 蜂窝蜜造平台生成代码，此文件只生成一次，不需要添加到 .beeignore

package com.fowo.api.service.impl;

import com.fowo.api.entity.ListingFather;
import com.fowo.api.entity.PurchasePlans;
import com.fowo.api.model.purchase.plans.PurchasePlansDetailedInfoParam;
import com.fowo.api.model.purchase.plans.PurchasePlansVo;
import com.fowo.api.sys.entity.SysUser;
import com.fowo.api.sys.model.NewSerialRequest;
import com.fowo.api.sys.service.impl.SysUserServiceImpl;
import com.fowo.api.user.model.JwtUserInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 采购计划 服务实现类
 */
@Service
public class PurchasePlansServiceImpl extends PurchasePlansBaseServiceImpl {
    @Resource
    private SysUserServiceImpl userService;
    @Resource
    private ListingFatherServiceImpl listingFatherService;

    @Override
    public void detailedInfo(PurchasePlansDetailedInfoParam param) {
        System.out.print("生成的信息11111111");
        this.baseMapper.detailedInfo(param);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Long create(PurchasePlansVo model) throws Exception {
        JwtUserInfo currentUser = JwtUserInfo.fromHeader();
        if(!StringUtils.isNotBlank(model.getCreateName2())){
            SysUser byId = userService.getById(currentUser.getUserId());
            model.setCreateName2(byId.getName());
        }
        model.getProcurementPlan().forEach(x->{
            if( x.getFnsku()!=null){
                ListingFather byId = listingFatherService.getById(x.getFnsku());
                x.setFnskuName(byId.getFnsku());
                x.setFnskuFnsku(byId.getFnsku());
            }
        });

        Long aLong = super.create(model);
        return aLong;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(PurchasePlansVo model) throws Exception {
        model.getProcurementPlan().forEach(x->{
            if( x.getFnsku()!=null){
                ListingFather byId = listingFatherService.getById(x.getFnsku());
                x.setFnskuName(byId.getFnsku());
                x.setFnskuFnsku(byId.getFnsku());
            }
        });
        boolean update = super.update(model);
        return update;
    }
}
